package ysq.xyg.ticket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.ticket.dao.TicketSupplierDao;
import ysq.xyg.ticket.model.po.TicketColumn;
import ysq.xyg.ticket.model.po.TicketSupplier;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.ticket.model.vo.TicketColumnVo;
import ysq.xyg.ticket.model.vo.TicketSupplierVo;
import ysq.xyg.ticket.service.TicketSupplierService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 票卷模块-供应商service实现
 *
 * @author shengQiang yu
 * @date 2019-10-24 10:29:50
 */
@Service("ticketSupplierService")
public class TicketSupplierServiceImpl extends BaseServiceImpl<TicketSupplier> implements TicketSupplierService {

    @Resource
    private TicketSupplierDao ticketSupplierDao;

    private TicketSupplier ticketSupplier;

    @Autowired
    public void setBaseDao(TicketSupplierDao ticketSupplierDao) {
        this.baseDao = ticketSupplierDao;
    }


    /**
     * 分页查询
     *
     * @param ticketSupplierVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:29:50
     */
    public ReturnEntity page(TicketSupplierVo ticketSupplierVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<TicketSupplier> list = acceptList(ticketSupplierVo);
        PageInfo<TicketSupplier> page=new PageInfo<TicketSupplier>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param ticketSupplierVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:29:50
     */
    public ReturnEntity addOrUpdate(TicketSupplierVo ticketSupplierVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(ticketSupplierVo.getId())){
            ticketSupplier=new TicketSupplier(currentUser);
            isUpate=false;
        }else{
            ticketSupplier=selectByPrimaryKey(ticketSupplierVo.getId());
            if(ObjectUtil.isNull(ticketSupplier)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ticketSupplier.setUpdateTime(new Date());
            ticketSupplier.setUpdateUser(currentUser.getId());
        }
        ticketSupplier.setLinkUser(ticketSupplierVo.getLinkUser());
        ticketSupplier.setLinkPhone(ticketSupplierVo.getLinkPhone());
        ticketSupplier.setDescription(ticketSupplierVo.getDescription());
        ticketSupplier.setCompanyName(ticketSupplierVo.getCompanyName());
        ticketSupplier.setAddress(ticketSupplierVo.getAddress());
        ticketSupplier.setSort(ticketSupplierVo.getSort());
        ticketSupplier.setShowType(ticketSupplierVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(ticketSupplier);
        }else{
            num=insert(ticketSupplier);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:29:50
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        TicketSupplier ticketSupplier=selectByPrimaryKey(id);
        if(ticketSupplier!=null){
            ticketSupplier.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ticketSupplier.setUpdateTime(new Date());
            ticketSupplier.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(ticketSupplier);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ticketSupplierVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-24 10:29:50
     */
    public List<TicketSupplier> acceptList(TicketSupplierVo ticketSupplierVo){
        Example example = new Example(TicketSupplier.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(ticketSupplierVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",ticketSupplierVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(ticketSupplierVo.getShowType())){
            criteria.andEqualTo("showType",ticketSupplierVo.getShowType());
        }
        if(StringUtil.isNotEmpty(ticketSupplierVo.getLinkPhone())){
            criteria.andLike("linkPhone","%"+ticketSupplierVo.getLinkPhone()+"%");
        }
        if(StringUtil.isNotEmpty(ticketSupplierVo.getLinkUser())){
            criteria.andLike("linkUser","%"+ticketSupplierVo.getLinkUser()+"%");
        }
        if(StringUtil.isNotEmpty(ticketSupplierVo.getCompanyName())){
            criteria.andLike("companyName","%"+ticketSupplierVo.getCompanyName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return ticketSupplierDao.selectByExample(example);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    @Override
    public ReturnEntity acceptSelect(String code) {
        List<TicketSupplier> list = acceptList(new TicketSupplierVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(TicketSupplier ticketSupplier:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(ticketSupplier.getId());
            sysDictContent.setName(ticketSupplier.getCompanyName()+"["+ticketSupplier.getLinkUser()+":"+ticketSupplier.getLinkPhone()+"]");
            sysDictContent.setDictType(code);
            sysDictContent.setValue(ticketSupplier.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
